package dbmanagement;

import java.util.ArrayList;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: [J0k3r]
 * Date: 26.11.2008
 * Time: 16:38:58
 */
public class FetchedObject implements Cloneable{
    private Integer id;
    private String name;
    private Boolean complex = false;
    private Boolean deleted = false;

    private ArrayList< FetchedTemplateParams > templateParams = new ArrayList<FetchedTemplateParams>();

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<FetchedTemplateParams> getTemplateParams() {
        return templateParams;
    }

    public void setTemplateParams(ArrayList<FetchedTemplateParams> templateParams) {
        this.templateParams = templateParams;
    }

    public FetchedParam getParameter(String parameterName) {
        for (FetchedTemplateParams templateParam : templateParams) {
            if (templateParam.containsParam(parameterName))
                return templateParam.getParam(parameterName);
        }
        return null;
    }

    public Boolean isComplex() {
        return this.complex;
    }

    public void setComplex(Boolean complex) {
        this.complex = complex;
    }

    public String toString() {
        String result = "";
        for (FetchedTemplateParams templateParam : templateParams)
            for (FetchedParam o : templateParam.getParams()) {
                String s = o.toString();
                if (s != null) result += (result.length() > 0 ? ", " : "") + s;
//                result += (result.length() > 0 ? ", " : "") + templateParam.getParams().get(o);
            }
//        result = ((this.getId() != null) ? this.getId() + ") " : "") + (((templateParams.size() > 0) && (templateParams.get(0).getTemplate() != null)) ? ("( " + templateParams.get(0).getTemplate().getName() + " ) ") : "( ? ) : ") + result;
        return result;
    }

    /*public String toStringNullIfUnassigned() {
        String result = "";
        for (FetchedTemplateParams templateParam : templateParams)
            for (String o : templateParam.getParams().keySet()) {
                String s = templateParam.getParams().get(o).toString();
                if (s != null) result += (result.length() > 0 ? ", " : "") + s;
            }
        result = ((this.getId() != null) ? this.getId() + ") " : "") + (((templateParams.size() > 0) && (templateParams.get(0).getTemplate() != null)) ? ("( " + templateParams.get(0).getTemplate().getName() + " )") : "( ? ) : ") + result;
        return result;
    }*/

    public String toMultilineString() {
        String result = "";
        for (FetchedTemplateParams templateParam : templateParams)
            for (FetchedParam o : templateParam.getParams())
                result += (result.length() > 0 ? "\n" : "") + o;
//        String firstLine = ((this.getId() != null) ? this.getId() + ") " : "") + (((templateParams.size() > 0) && (templateParams.get(0).getTemplate() != null)) ? ("( " + templateParams.get(0).getTemplate().getName() + " )") : "( ? ) : ");
//        result = (firstLine.length() > 0 ? firstLine + "\n" : "") + result;
        return result;
    }

    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setParameter(String parameterName, Object value) {
        for (FetchedTemplateParams templateParam : templateParams) {
            FetchedParam paramWithName = templateParam.getParam(parameterName);
            if (paramWithName != null) paramWithName.setValue(value);
        }
    }

    public Object clone() throws
            CloneNotSupportedException {
        FetchedObject fetchedObject = new FetchedObject();
        ArrayList<FetchedTemplateParams> templateParams = new ArrayList<FetchedTemplateParams>();
        for (FetchedTemplateParams templateParam : this.templateParams)
            templateParams.add((FetchedTemplateParams) templateParam.clone());
        fetchedObject.setComplex(new Boolean(complex));
        fetchedObject.setDeleted(new Boolean(deleted));
        fetchedObject.setId(id != null ? new Integer(id) : null);
        fetchedObject.setName(name != null ? new String(name) : null);
        fetchedObject.setTemplateParams(templateParams);
        return fetchedObject;
    }
}
